//
//  SCNMoneyGuideCell1.swift
//  ShoucainuSwift
//
//  Created by 王家旭 on 2017/11/17.
//  Copyright © 2017年 Riluee. All rights reserved.
//

import UIKit

class SCNMoneyGuideCell1: UICollectionViewCell {
    
    // typealias 可以给任意类型弄个别名
    typealias MoneyGuideTopAction = (String) -> Void
    // 给闭包声明变量
    var MoneyGuideTopClosures: MoneyGuideTopAction?
    
    lazy var progressBGImgView: UIImageView = {
        let img = UIImageView()
        return img
    }()
    lazy var infoLable: UILabel = {
        let lab = UILabel()
        lab.font = UIFont.systemFont(ofSize: 14.0)
        lab.textColor = SCNConstants.colorWithHexString("898989")
        lab.numberOfLines = 0
        lab.lineBreakMode = .byCharWrapping
        return lab
    }()
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = .white
        
        configurationUI()
    }
    // MARK: UI
    func configurationUI() {
        
        let titleArray = ["守财奴计划","秒杀计划","体验标","新手专享"]
        let interval = 5
        let widthBtn = (SCNConstants.SCREENWIDTH - 30 - 15)/4
        
        for index in 0..<4 {
            let button = UIButton()
            button.backgroundColor = SCNConstants.colorWithHexString("f0f0f0")
            button.frame = CGRect(x: 15 + Int(widthBtn) * index + interval * index, y: 15, width: Int(widthBtn), height: 35)
            button.titleLabel?.font = UIFont.systemFont(ofSize: 14.0)
            button.tag = 20 + index
            button.setTitle(titleArray[index], for: .normal)
            button.setTitleColor(SCNConstants.colorWithHexString("574d4c"), for: .normal)
            button.addTarget(self, action: #selector(topButtonAction(_:)), for: .touchUpInside)
            self.contentView.addSubview(button)
            
            if index == 0 {
                button.backgroundColor = SCNConstants.rootBackgroundColor
                button.setTitleColor(.white, for: .normal)
                let tmp = addRounded(UIRectCorner(rawValue: UIRectCorner.RawValue(UInt8(UIRectCorner.topLeft.rawValue) | UInt8(UIRectCorner.bottomLeft.rawValue))), radii: CGSize(width: 5, height: 5), rect: button.bounds)
                button.layer.mask = tmp
            }
            if index == 3 {
                let tmp = addRounded(UIRectCorner(rawValue: UIRectCorner.RawValue(UInt8(UIRectCorner.topRight.rawValue) | UInt8(UIRectCorner.bottomRight.rawValue))), radii: CGSize(width: 5, height: 5), rect: button.bounds)
                button.layer.mask = tmp
            }
        }
        
        
        self.contentView.addSubview(progressBGImgView)
        progressBGImgView.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(70)
            make.size.equalTo(CGSize(width: 170, height: 170))
        }
        self.contentView.addSubview(infoLable)
        infoLable.snp.makeConstraints { (make) in
            make.leading.equalTo(10)
            make.trailing.equalTo(-10)
            make.top.equalTo(progressBGImgView.snp.bottom).offset(20)
        }
        
        let line = UILabel()
        line.backgroundColor = SCNConstants.colorWithHexString("dddddd")
        self.contentView.addSubview(line)
        line.snp.makeConstraints { (make) in
            make.leading.equalTo(20)
            make.trailing.equalTo(-20)
            make.height.equalTo(0.5)
            make.bottom.equalTo(-30)
        }
        let hintLab = UILabel()
        hintLab.text = "上拉滑动，查看更多详情"
        hintLab.textColor = SCNConstants.colorWithHexString("c9c9c9")
        hintLab.font = UIFont.systemFont(ofSize: 14.5)
        hintLab.backgroundColor = .white
        self.contentView.addSubview(hintLab)
        hintLab.sizeToFit()
        hintLab.snp.makeConstraints { (make) in
            make.center.equalTo(line.snp.center)
        }
        let arrowsImgView = UIImageView(image: UIImage(named: "icon-shangla"))
        self.contentView.addSubview(arrowsImgView)
        arrowsImgView.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 12,height: 12))
            make.centerX.equalTo(line.snp.centerX)
            make.centerY.equalTo(line.snp.centerY).offset(-25)
        }
    }
    
    public func setMoneyGuideTableViewCell1(_ model: SCNMoneyGuideDataresultModels, index: IndexPath) {
        
        if let tmpimg = model.img,let tmptext = model.text {
            infoLable.text = tmptext
            let att = NSMutableAttributedString.init(string: infoLable.text!, attributes: [NSKernAttributeName : 1.0])
            let para = NSMutableParagraphStyle()
            para.lineSpacing = 2
            att.addAttribute(NSParagraphStyleAttributeName, value: para, range: NSRange(location: 0, length: (infoLable.text?.characters.count)!))
            infoLable.attributedText = att
            progressBGImgView.kf.setImage(with: URL(string:"\(SCNRequestAPI.ServiceAPI)\(tmpimg)"))
        }
        
    }
    func topButtonAction(_ sender: UIButton) {
        self.contentView.subviews.forEach { (view) in
            if view.isKind(of: UIButton.self) {
                let tmp = view as! UIButton
                tmp.backgroundColor = SCNConstants.colorWithHexString("f0f0f0")
                tmp.setTitleColor(SCNConstants.colorWithHexString("574d4c"), for: .normal)
            }
        }
        sender.backgroundColor = SCNConstants.rootBackgroundColor
        sender.setTitleColor(.white, for: .normal)
        
        var tmpkey = ""
        switch sender.tag {
        case 20:
            tmpkey = "a"
        case 21:
            tmpkey = "b"
        case 22:
            tmpkey = "c"
        case 23:
            tmpkey = "d"
        default:
            tmpkey = "a"
        }
        MoneyGuideTopClosures?(tmpkey)
    }
    // 切圆角
    func addRounded(_ corners: UIRectCorner,radii: CGSize,rect: CGRect) -> CAShapeLayer {
        let rounded = UIBezierPath(roundedRect: rect, byRoundingCorners: corners, cornerRadii: radii)
        let shape = CAShapeLayer()
        shape.path = rounded.cgPath
        return shape
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
